<template>
	<view>
		<view style="background-color: #FFFFFF;">
			<cu-custom :isBack="true">
				<block slot="backText"></block>
				<block slot="content">{{corpCode}}-货位盘点列表</block>
			</cu-custom>
		</view>
		<view class="cu-bar bg-white search fixed indexes" :style="[{top:CustomBar + 'px'}]">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input @confirm="getData" v-model="inputVal" type="text" placeholder="输入关键词" focus></input>
			</view>
			<view class="action" @click="toScan">
				<text class="cuIcon-scan" style="font-size: 56rpx;"></text>
			</view>
		</view>
		<view class="cu-bar bg-white solid-bottom margin-top" style="margin-top: 13%;">
			<view class="action">
				<text class="cuIcon-title text-orange"></text>列表 {{cdata == ''?'':'-'+cdata.djno}} {{cdata == ''?'':'-'+cdata.basWarehouse.cwhname}}
			</view>
		</view>
		<scroll-view scroll-y="true" class="indexes"  :scroll-with-animation="true"
		 :enable-back-to-top="true" :style="[{height:'calc(100vh - '+ CustomBar + 'px - 100px)'}]">
			<view class="cu-list menu">
				<view class="cu-item" v-for="(item,index) in posList" :key="index">
					<view class="content">
						<text class="cuIcon-post text-green"></text>
						<text class="text-grey">{{item.cposname}}{{item.operName!=''?' -- '+item.operName:''}}</text>
					</view>
					<view class="action">
						<button class="cu-btn round bg-green" @click="fromPosData(index)" v-if="item.isData">
							<text class="cuIcon-search"></text> 查看</button>
						
						<button class="cu-btn round bg-red" @click="fromPos(index)">
							<text class="cuIcon-post"></text> 盘点</button>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				menuArrow:true, 
				CustomBar: this.CustomBar, //滚动定位用
				inputVal:"", //扫码值
				cdata:'',   //任务列表
				hwcode:'', //货位编码
				hwname:'', //货位名称
				posList:[],
				vals:'',
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'auditlist', 'corpCode']),
		},
		onShow() {
			if(this.vals != ''){
				this.inputVal = this.vals;
				if(this.inputVal != ''){
					this.getData();
				}
			}
		},
		onLoad() {
			// this.inputVal = 'PDRW_1471655821101350912';
			// this.getData();
		},
		methods: {
			/** 获取数据 扫描盘点任务单，获取当前盘点人需盘点货位列表*/
			getData(){
				this.posList = [];
				this.cdata = '';
				//1、盘点任务单获取完成后，扫描货位可直接跳转该货位明细
				if(this.inputVal.indexOf("hw") > -1){
					this.getPostData();
					return;
				}else if(this.inputVal.indexOf("PDRW") > -1){//2、盘点任务单数据获取
					this.getCheckPosData();
					return;
				}
				this.inputVal = "";
			},
			
			/** 获取货位信息*/
			getPostData(){
				uni.request({
					url:this.serverURL_a+'/so/App/getHw',
					data:{
						hw : this.inputVal,
						__sid:this.userInfo.sessionid
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					method: 'POST',
					success:function(data){
						this.inputVal = '';
						var requrt = data.data;
						if(requrt.code == '1001'){
							var hwdata = requrt.resMap;
							this.hwcode = hwdata.cposcode;
							this.hwname = hwdata.cposname;
						}else{
							uni.showModal({
							    content: requrt.msg,
								showCancel:false,
							});
							return;
						}
					}.bind(this),
					fail:function(){
						this.$api.msg('服务已停止,请稍后再试');
						return;
					}.bind(this),
				});
			},
			
			/** 获取盘点信息*/
			getCheckPosData(){
				uni.request({
					url:this.serverURL_a+'/wh/check/api/whCheckinvTask/findCheckPosData',
					data:{
						id : this.inputVal,
						__sid:this.userInfo.sessionid
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					method: 'POST',
					success:function(data){
						this.vals = this.inputVal;
						this.inputVal = '';
						if(data.statusCode!="200"){
							if(data.statusCode == "403"){
								uni.showModal({
								    content: "权限不足!",
									showCancel:false,
								});
							}
							uni.showModal({
							    content: "其他异常!",
								showCancel:false,
							});
							return;
						}
						var result = data.data;
						if(result.result == "true"){
							//分装数据
							this.cdata = result.data;
							this.setDataByResult(result.data);
						}else{
							uni.showModal({
							    content: result.message,
								showCancel:false,
							});
						}
					}.bind(this),
					fail:function(){
						this.$api.msg('服务已停止,请稍后再试');
						return;
					}.bind(this),
				});
			},
			
			/** 分装信息*/
			setDataByResult(data){
				this.posList = [];
				var list = data.posList;
				//货位循环
				for(var i=0;i<list.length;i++){
					var pos = {
						cposcode:list[i].basPosition.cposcode,
						cposname:list[i].basPosition.cposname,
						viewCode:list[i].basPosition.viewCode,
						isData:false,
						operName:'',
					}
					this.posList.push(pos);
				}
				//寻找有盘点记录的货位
				for(var i=0;i<this.posList.length;i++){
					var rdrlist = data.rdsList;
					for(var j=0;j<rdrlist.length;j++){
						if(this.posList[i].cposcode == rdrlist[j].basPosition.cposcode){
							this.posList[i].isData = true;
							this.posList[i].operName = rdrlist[j].operUser.userName;
							break;
						}
					}
				}
			},
			
			/** 扫码*/
			toScan(){
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						var rdata = res.result;
						this.inputVal = rdata;
						setTimeout(function(){
							this.getData();
						}.bind(this),100);
					}.bind(this)
				});
			},
			
			/** 转转页面至明细查看数据*/
			fromPosData(index){
				uni.navigateTo({
					url: '/pages/posJustInv/posCheckInvSreach?code='+this.posList[index].cposcode+'&id='+this.cdata.id
				})
			},
			
			/** 跳转页面至盘点明细*/
			fromPos(index){
				
				if(this.posList[index].operName == ''){
					if(this.cdata.selOperNames.indexOf(this.userInfo.user.userName) <= -1){
						uni.showModal({
							content: '你不是该任务单盘点人,是否继续盘点!',
							success: function(res) {
								if(res.confirm){
									uni.navigateTo({
										url: '/pages/posJustInv/posCheckInvFrom?code='+this.posList[index].cposcode+'&id='+this.cdata.id
									})
								}else{
									this.ishow = false;
								}
							}.bind(this)
						});
					}else{
						uni.navigateTo({
							url: '/pages/posJustInv/posCheckInvFrom?code='+this.posList[index].cposcode+'&id='+this.cdata.id
						})
					}
				}else if(this.userInfo.user.userName == this.posList[index].operName){
					uni.navigateTo({
						url: '/pages/posJustInv/posCheckInvFrom?code='+this.posList[index].cposcode+'&id='+this.cdata.id
					})
				}else{
					this.$api.msg('该货位已被盘点，请选择其他货位盘点!');
				}
			}
		}
	}
</script>

<style>
.page {
		height: 100Vh;
		width: 100vw;
	}

	.page.show {
		overflow: hidden;
	}

	.switch-sex::after {
		content: "\e716";
	}

	.switch-sex::before {
		content: "\e7a9";
	}

	.switch-music::after {
		content: "\e66a";
	}

	.switch-music::before {
		content: "\e6db";
	}
</style>
